import { getUserProfile } from '@/api'
import type { AccessMapReturn, AccessMenuItem } from '@/types/services'
import type { RouteRecordRaw } from 'vue-router'

const modules = import.meta.glob('@/views/**/*View.vue')

export const getImportComponent = (path: string) => {
  return modules[`/src/views/${path}.vue`]
}

export const getRoutes = (): RouteRecordRaw[] => [
  {
    path: 'home',
    name: 'home',
    component: getImportComponent('home/HomeView'),
    meta: {
      access: 'homeRoute'
    }
  },
  {
    path: 'role',
    name: 'role',
    component: getImportComponent('role/RoleView'),
    meta: {
      access: 'roleListRoute'
    }
  },
  {
    path: 'role/add',
    name: 'role-add',
    component: getImportComponent('role/SaveView'),
    meta: {
      access: 'roleAddRoute'
    }
  },
  {
    path: 'role/save',
    name: 'role-save',
    component: getImportComponent('role/SaveView'),
    meta: {
      access: 'roleEditRoute'
    }
  },
  {
    path: 'user',
    name: 'user',
    component: getImportComponent('user/UserView'),
    meta: {
      access: 'userListRoute'
    }
  },
  {
    path: 'user/add',
    name: 'user-add',
    component: getImportComponent('user/SaveView'),
    meta: {
      access: 'userAddRoute'
    }
  },
  {
    path: 'user/save',
    name: 'user-save',
    component: getImportComponent('user/SaveView'),
    meta: {
      access: 'userEditRoute'
    }
  },

  {
    path: 'permission',
    name: 'permission',
    component: getImportComponent('permission/PermissionView'),
    meta: {
      access: 'permissionListRoute'
    }
  },
  {
    path: 'permission/add',
    name: 'permission-add',
    component: getImportComponent('permission/SaveView'),
    meta: {
      access: 'permissionAddRoute'
    }
  },
  {
    path: 'permission/save',
    name: 'permission-save',
    component: getImportComponent('permission/SaveView'),
    meta: {
      access: 'permissionEditRoute'
    }
  }
]

export const getMenus = (): AccessMenuItem[] => [
  {
    name: 'home',
    icon: 'House',
    title: '控制台',
    access: 'homeMenu',
    full: false,
    children: []
  },
  {
    name: 'access',
    icon: 'Lock',
    title: '权限管理',
    access: 'accessMenu',
    full: false,
    children: [
      {
        name: 'user',
        icon: 'User',
        title: '用户列表',
        access: 'userListMenu',
        full: false,
        children: []
      },
      {
        name: 'role',
        icon: 'Avatar',
        title: '角色列表',
        access: 'roleListMenu',
        full: false,
        children: []
      },
      {
        name: 'permission',
        icon: 'Warning',
        title: '权限列表',
        access: 'permissionMenu',
        full: false,
        children: []
      }
    ]
  }
]

export const getAccessMap = async (): Promise<AccessMapReturn> => {
  const { id, roles, permissions } = await getUserProfile()
  return {
    homeRoute: true,
    userListRoute: true,
    userAddRoute: true,
    userEditRoute: true,
    roleListRoute: true,
    roleAddRoute: true,
    roleEditRoute: true,
    permissionListRoute: true,
    permissionAddRoute: true,
    permissionEditRoute: true,
    homeMenu: true,
    accessMenu: true,
    userListMenu: true,
    roleListMenu: true,
    permissionMenu: true
    // canReadFoo: true
    // canUpdateFoo: role === 'admin',
    // canDeleteFoo: (foo) => {
    //   return foo.ownerId === userId
    // }
  }
}

export const getAccessMenus = async (): Promise<AccessMenuItem[]> => {
  const menus = getMenus()
  const accessMap = await getAccessMap()

  const deletes: { arr: AccessMenuItem[]; del: number }[] = []
  const recursion = (ml: AccessMenuItem[]) => {
    ml.forEach((m, i) => {
      if (m.children.length > 0) {
        recursion(m.children)
      }
      if (!(m.access in accessMap && accessMap[m.access])) {
        deletes.push({
          arr: ml,
          del: i
        })
      }
    })
  }

  recursion(menus)

  deletes.sort((a, b) => b.del - a.del).forEach(({ arr, del }) => arr.splice(del, 1))

  return menus
}

export const getAccessRoutes = async (): Promise<RouteRecordRaw[]> => {
  const routes = getRoutes()
  const accessMap = await getAccessMap()

  const deletes: { arr: RouteRecordRaw[]; del: number }[] = []
  const recursion = (rl: RouteRecordRaw[]) => {
    rl.forEach((r, i) => {
      if (r.children && r.children.length > 0) {
        recursion(r.children)
      }
      const access = r.meta?.access as string | undefined
      if (!(access && access in accessMap && accessMap[access])) {
        deletes.push({
          arr: rl,
          del: i
        })
      }
    })
  }

  recursion(routes)

  deletes.sort((a, b) => b.del - a.del).forEach(({ arr, del }) => arr.splice(del, 1))

  return routes
}
